package com.xmut.mall.order.controller;

import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmut.mall.order.annotation.CheckJwt;
import com.xmut.mall.order.searchparam.ManagerOrderSearchParam;
import com.xmut.mall.order.searchparam.OrderSearchParams;
import com.xmut.mall.order.searchparam.SellerOrderSearchParam;
import com.xmut.mall.order.to.OrderDeliveryDto;
import com.xmut.mall.order.to.UpdateOrderConsigneeDto;
import com.xmut.mall.order.utils.RespBean;
import com.xmut.mall.order.vo.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xmut.mall.order.entity.OrderEntity;
import com.xmut.mall.order.service.OrderService;
import com.xmut.mall.order.utils.PageUtils;
import com.xmut.mall.order.utils.R;

@RestController
@RequestMapping("order/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @CheckJwt
    @GetMapping("/getOrderBySn/{sn}")
    public RespBean getOrderBySn(@PathVariable String sn){
        OrderEntity order = orderService.getOne(new QueryWrapper<OrderEntity>().eq("sn", sn));
        if (order==null){
            return RespBean.error("订单不存在");
        }else {
            return RespBean.success("获取成功",order);
        }
    }

    /**
     * 用户确认收货
     * @param params
     * @return
     */
    @CheckJwt
    @PostMapping("/userReceiveOrder")
    public RespBean userReceiveOrder(@RequestBody Map<String,Object> params){
        return orderService.userReceiveOrder(params);
    }

    /**
     * 用户取消订单
     * @param params
     * @return
     */
    @CheckJwt
    @PostMapping("/userCancelOrder")
    public RespBean userCancelOrder(@RequestBody Map<String,Object> params){
        return orderService.userCancelOrder(params);
    }

    /**
     * 官方管理平台取消订单
     * @param params
     * @return
     */
    @CheckJwt
    @PostMapping("/ManagerCancelOrder")
    public RespBean ManagerCancelOrder(@RequestBody Map<String,Object> params){
        return orderService.ManagerCancelOrder(params);
    }

    /**
     * 获取物流信息
     * @param sn
     * @return
     */
    @CheckJwt
    @GetMapping("/getTraces/{sn}")
    public RespBean getTraces(@PathVariable String sn){
        return orderService.getTraces(sn);
    }

    /**
     * 订单发货
     * @param dto
     * @return
     */
    @CheckJwt
    @PostMapping("/orderDelivery")
    public RespBean orderDelivery(@RequestBody OrderDeliveryDto dto){
        return orderService.orderDelivery(dto);
    }

    /**
     * 修改订单收货地址
     * @param dto
     * @return
     */
    @CheckJwt
    @PostMapping("/updateOrderConsignee")
    public RespBean updateOrderConsignee(@RequestBody UpdateOrderConsigneeDto dto){
        return orderService.updateOrderConsignee(dto);
    }

    /**
     * 修改订单价格
     * @param params
     * @return
     */
    @CheckJwt
    @PostMapping("/updateOrderPrice")
    public RespBean updateOrderPrice(@RequestBody Map<String,Object> params){
        return orderService.updateOrderPrice(params);
    }

    /**
     * 获取商城官方平台的订单分页数据
     * @param param
     * @return
     */
    @CheckJwt
    @PostMapping("/getManagerOrderByPage")
    public RespBean getManagerOrderByPage(@RequestBody ManagerOrderSearchParam param){
        PageVO vo  = new PageVO();
        BeanUtils.copyProperties(param,vo);
        return orderService.getManagerOrderByPage(vo,param);
    }

    /**
     * 获取卖家端的订单分页数据
     */
    @CheckJwt
    @PostMapping("/getSellerOrderByPage")
    public RespBean getSellerOrderByPage(@RequestBody SellerOrderSearchParam param){
        PageVO vo = new PageVO();
        BeanUtils.copyProperties(param,vo);
        return orderService.getSellerOrderByPage(vo,param);
    }

    /**
     * 获取订单详情
     * @return
     */
    @CheckJwt
    @PostMapping("/getOrderDetail")
    public RespBean getOrderDetail(@RequestBody Map<String,Object> params) {
        return orderService.getOrderDetail(params);
    }

    /**
     * 获取用户中心中的订单分页数据
     *
     * @param params
     * @return
     */
    @CheckJwt
    @PostMapping("/getBuyerOrderByPage")
    public RespBean getBuyerOrderByPage(@RequestBody OrderSearchParams params) {
        PageVO vo = new PageVO();
        BeanUtils.copyProperties(params, vo);
        return orderService.getBuyerOrderByPage(vo, params);
    }

    /**
     * 保存订单
     *
     * @param entity
     * @return
     */
    @CheckJwt
    @PostMapping("/createOrder")
    public RespBean createOrder(@RequestBody OrderEntity entity) {
        return orderService.createOrder(entity);
    }

    /**
     * 订单因为超时为支付而取消
     *
     * @param param
     */
    @CheckJwt
    @PostMapping("/cancelOrderReasonTimeout")
    public void cancelOrderReasonTimeout(@RequestBody Map<String, Object> param) {
        orderService.cancelOrderReasonTimeout(param);
    }
}
